<?php

namespace app\api\controller;

use app\api\service\LoginService;
use app\api\service\UserAmountService;
use app\BaseController;
use app\Response;
use think\facade\Request;

class UserBank extends BaseController
{
    /*** 绑定银行卡 /支付宝
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function bankAdd(){
        if (Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }
            $data = $this->request->post();
            try{
                $this->validate($data, 'UserBankAdd');
            }catch (\Exception $e){
                return Response::wrapError($e->getMessage());
            }
            $bankCard=addslashes($data['bankCard']);//假设$lxfs参数值有可能是手机号或邮箱地址
            if(is_numeric($bankCard)){//如果传来的是数字则判断是否是手机
                if(!preg_match('/^1[3456789]{1}\d{9}/',$bankCard) && $data['bankType'] == 2){
                    return Response::wrapError('错误的手机号码');
                }
            }else{//否则判断是否是邮箱
                if(!filter_var($bankCard,FILTER_VALIDATE_EMAIL) && $data['bankType'] == 2){
                    return Response::wrapError('错误的邮箱');
                }
            }
            $result = UserAmountService::bankAddService($user_id,$data);
            return $result;
        }
    }

    /*** 解绑银行卡 或者支付宝
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function bankDel(){
        if (Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }
            $data = $this->request->post();
            try{
                $this->validate($data, 'UserBankDel');
            }catch (\Exception $e){
                return Response::wrapError($e->getMessage());
            }
            $result = UserAmountService::bankDelService($user_id,$data);
            return $result;
        }
    }
}